﻿using System;
using System.Windows.Forms;
using S3.Biz;
using S3.Common;

namespace S3
{
    public partial class SwitchToAnotherDesk : S3FormBase
    {
        private readonly string _currentKindOfMachineTab1;
        private string _currentKindOfMachineTab2;

        public SwitchToAnotherDesk(string currentDeskName, string desDeskName, string[] listDesKindOfMachine,
                                   object oldKindOfMachinePlaying)
        {
            _currentKindOfMachineTab1 = oldKindOfMachinePlaying + "";

            InitializeComponent();
            LoadData(currentDeskName, desDeskName, listDesKindOfMachine, oldKindOfMachinePlaying);
        }

        private void LoadData(string currentDeskName, string desDeskName, string[] listDesKindOfMachine,
                              object oldKindOfMachinePlaying)
        {
            #region From

            lblDestinationDeskName_From.Text = desDeskName;
            xtraTabControl.TabPages[0].Text = currentDeskName;
            listBoxKindOfMachine_From.DataSource = listDesKindOfMachine;
            foreach (string item in listDesKindOfMachine)
            {
                if (!item.Equals(oldKindOfMachinePlaying))
                {
                    listBoxKindOfMachine_From.SelectedItem = item;
                }
            }

            #endregion

            #region To

            xtraTabControl.TabPages[1].Text = desDeskName;
            bool isPlaying = false;
            using (var dailyEntryManager = new DailyTimingEntryManager())
            {
                isPlaying = dailyEntryManager.IsPlaying(desDeskName);
                if (isPlaying)
                {
                    _currentKindOfMachineTab2 = dailyEntryManager.GetOnePlaying(desDeskName).Type;
                }
            }

            if (!isPlaying)
            {
                xtraTabControl.TabPages[1].Controls.Clear();
            }
            else
            {
                lblDestinationDeskName_To.Text = currentDeskName;

                using (var deskConfigurationManager = new TimingServiceConfigurationManager())
                {
                    //Load Kind of machine
                    string[] currentKindOfMachineList = deskConfigurationManager.GetKindOfMachineByDesk(currentDeskName);
                    listBoxKindOfMachine_Des.DataSource = currentKindOfMachineList;
                    foreach (string desitem in currentKindOfMachineList)
                    {
                        if (desitem.Equals(oldKindOfMachinePlaying))
                        {
                            listBoxKindOfMachine_Des.SelectedItem = oldKindOfMachinePlaying;
                        }
                    }
                }
            }

            #endregion
        }

        private void btnDone_Click(object sender, EventArgs e)
        {
            MainPaneForm._switchAndStopCurrentPlaying_From = chkStopCurrentPlaying_From.Checked;
            MainPaneForm._switchdToDesinationKindOfMachine_From = listBoxKindOfMachine_From.SelectedValue + "";
            MainPaneForm._switchAndStopCurrentPlaying_To = chkStopCurrentPlaying_To.Checked;
            MainPaneForm._switchdToDesinationKindOfMachine_To = listBoxKindOfMachine_Des.SelectedValue + "";
            DialogResult = DialogResult.OK;
            Close();
        }

        private void listBoxKindOfMachine_From_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxKindOfMachine_From.SelectedItem.Equals(_currentKindOfMachineTab1))
            {
                chkStopCurrentPlaying_From.Checked = false;
                chkStopCurrentPlaying_From.Enabled = false;
            }
            else
            {
                chkStopCurrentPlaying_From.Enabled = true;
            }
        }

        private void listBoxKindOfMachine_Des_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxKindOfMachine_Des.SelectedItem.Equals(_currentKindOfMachineTab2))
            {
                chkStopCurrentPlaying_To.Checked = false;
                chkStopCurrentPlaying_To.Enabled = false;
            }
            else
            {
                chkStopCurrentPlaying_To.Enabled = true;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
    }
}